package com.yvan.cs.utils;

import com.google.common.collect.Maps;
import lombok.SneakyThrows;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;

import java.util.Map;

public class BeetlUtils {

    public static final GroupTemplate groupTemplate;

    static {
        try {
            StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
            Configuration cfg = Configuration.defaultConfiguration();
            groupTemplate = new GroupTemplate(resourceLoader, cfg);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @SneakyThrows
    public static String generator(String template, Map<String, Object> map) {
        Template t = groupTemplate.getTemplate(template);
        t.binding(map);
        return t.render();
    }

    public static void main(String[] args) {
        //FileResourceLoader loader = new FileResourceLoader();
        //loader.setRoot("./codeslave/templates");
        String t = "hello,${name}";
        Map<String, Object> maps = Maps.newLinkedHashMap();
        maps.put("name", "罗一帆");
        System.out.println(generator(t, maps));
    }
}
